package com.whmall.service.crm.module.companycertificatechemical.impl;

import com.whmall.component.util.FlatListUtils;
import com.whmall.service.crm.module.companybusinesscertificate.dao.CompanyBusinessCertificateMapper;
import com.whmall.service.crm.module.companybusinesscertificate.entity.CompanyBusinessCertificateDO;
import com.whmall.service.crm.module.companybusinesscertificate.entity.qo.CompanyBusinessCertificateQO;
import com.whmall.service.crm.module.companycertificatechemical.entity.CompanyCertificateChemicalApproveDO;
import com.whmall.service.crm.module.companycertificatechemical.entity.CompanyCertificateChemicalDO;
import com.whmall.service.crm.module.companycertificatechemical.enumeration.ApproveStatusEnum;
import com.whmall.service.crm.module.companycertificatechemical.impl.dao.CompanyCertificateChemicalMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 公司证照化合物审批生成器
 *
 * @author alex
 * @since 2020/12/19
 */
@Component
@RequiredArgsConstructor
public class CompanyCertificateChemicalApproveGenerator {
    private final CompanyBusinessCertificateMapper certificateMapper;
    private final CompanyCertificateChemicalMapper companyCertificateChemicalMapper;

    /**
     * 根据公司证照ID列表生成审批列表
     *
     * @param certificateIds 公司证照ID列表
     * @return 根据证照ID列表生成的审批列表，注意如果该证照没有关联的化合物则不会生成返回
     */
    public List<CompanyCertificateChemicalApproveDO> generateFromAssociatedCertificateIds(List<Long> certificateIds) {
        List<CompanyBusinessCertificateDO> certificates = certificateMapper.listBy(CompanyBusinessCertificateQO.builder()
                .idIn(certificateIds)
                .build());
        if (certificates.isEmpty()) {
            return Collections.emptyList();
        }

        Map<Long, List<CompanyCertificateChemicalDO>> certificateAssociatedMap = companyCertificateChemicalMapper.listByCertificateIds(certificateIds)
                .stream()
                .collect(Collectors.groupingBy(CompanyCertificateChemicalDO::getCertificateId));

        return certificates.stream()
                .map(e -> {
                    List<CompanyCertificateChemicalDO> associatedChemicalList = certificateAssociatedMap.get(e.getId());
                    if (null == associatedChemicalList || associatedChemicalList.isEmpty()) {
                        return null;
                    }

                    CompanyCertificateChemicalApproveDO approve = new CompanyCertificateChemicalApproveDO();
                    approve.setId(e.getId());
                    approve.setCompanyId(e.getCompanyId());
                    approve.setCertificateType(e.getCertificateType());
                    approve.setChemicalIdList(FlatListUtils.toFlatString(",", associatedChemicalList.stream()
                            .map(CompanyCertificateChemicalDO::getChemicalId)
                            .collect(Collectors.toList())));
                    approve.setVerifiedList(analyzeVerifiedListFromAssociated(associatedChemicalList));
                    return approve;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    protected String analyzeVerifiedListFromAssociated(List<CompanyCertificateChemicalDO> associated) {
        List<ApproveStatusEnum> approveStatusList = associated.stream()
                .map(e -> ApproveStatusEnum.fromValue(e.getVerified()))
                .distinct()
                .collect(Collectors.toList());

        return FlatListUtils.toFlatString(",", approveStatusList.stream()
                .map(ApproveStatusEnum::getValue)
                .collect(Collectors.toList()));
    }
}
